<?php
namespace Home\Controller;
use \Think\Controller;

class PublicController extends Controller{

	public function index(){

		$this->display('index');
	}


	public function verify(){
			$Verify = new \Think\Verify();
			$Verify->fontSize = 40;
			$Verify->length = 4;
			$Verify->useNoise = false;
			$Verify->entry();
	}
	public function deal(){
		$id = I('get.id/d',0);
		 
		 
		$district = M('district')->where("upid=$id")->select();
	      
		// $dthis->assign('dis',$district);
		 $this->ajaxReturn($district);
		 // echo json_encode($district);

	}
	public function register(){
		$this->display('register');
	}


	// 检测输入的验证码是否正确，$code为用户输入的验证码字符串
	public	function check_verify(){
		//	$code =$_GET['a'];
			 $code = I('get.code');
				$verify = new \Think\Verify();
				$code= $verify->check($code);
				$this->ajaxReturn($code);
			}

		// jalx 验证账号
	public function dovalidation(){
		$id =I('get.id');
		$data = M('user')->where("name='$id'")->select();
		if(!empty($data[0]['id'])){
			$this->ajaxReturn('error');
		}else{
			$this->ajaxReturn('ok');
		}
	}

	//执行修改
	public function update(){
		$uid = I('get.uid/d',0);
		$iid = I('get.iid/d',0);
		dump($_POST);
		M('user')->create() ;   //创建数据 获取post数据；
		M('userinfo')->create() ;   //创建数据 获取post数据；
		M('user')->where("id=$uid")->save();
			if(M('userinfo')->where("id=$iid")->save()>0){
				$this->success('恭喜您，修改成功', U('index'));
			}else{
				echo 1;
				exit;
				$this->error('修改失败！');
			}
	}
	//数据添加操作
	public function insert(){
		 	
		 	$data['name'] = __ROOT__.'/Public/Images/touxiang.jpg';
			$data['is_face'] = 1;
		$_POST['utime'] =time();
		$pas = $_POST['password'];
		$preg='/^[a-zA-Z\x{4e00}-\x{9fa5}]{4,10}$/u';
		$preg1='/^[\w\_]{6,20}$/u';
		if(!preg_match($preg1,$pas)){
			 $this->error('添加失败,中文和英文结合而且6-20字符1');
					 exit;
		}
		$uname = $_POST['name'];
		if(!preg_match($preg,$uname)){
			 $this->error('添加失败,中文和英文结合而且6-20字符2');
					 exit;
		}


		$_POST['password'] = md5($_POST['password']);
		 $_POST['integral'] = empty($_SESSION['rule'])?'100':$_SESSION['rule'];
		 
		M('user')->create();
		$a=M('user')->add();
		$ral['uid']=$a;
		$ral['type'] = 1;
		$ral['value'] =  empty($_SESSION['rule'])?'100':$_SESSION['rule'];
		$ral['reason'] = '注册赠送积分';
		$ral['utime'] = time();
		if($a>0){
			$_POST['uid'] = $a;
			$data['uid'] = $a;
			M('userinfo')->create();
			if(M('userinfo')->add() > 0){

				if((M('userphoto')->add($data) >0) && (M('integral')->add($ral) >0) ){

					$this->success('恭喜您，添加成功',U('index'));
					exit;
				}
					 $this->error('恭喜您，添加失败');
					 exit;
			}else{
				   $this->error('恭喜您，添加失败');
			}
		}else{
			  $this->error('恭喜您，添加失败');
		}
	}
	public function edit(){
		$iid = I('get.iid/d',0);
		$nid = I('get.nid/d',0);
		$data1 = M('user')->find($nid);
		$data2 = M('userinfo')->find($iid);
		if($data1 ==null || $data2 == null){
			$this->redirect('index');
			return;
		}
		$this->assign('data1',$data1);
		$this->assign('data2',$data2);
		$this->display();
	}

		// 执行注册
	public function validation(){
		 $name= I('post.name',0);
		 $id= I('post.id',0);
		 $sex= I('post.sex',0);
		 $password= I('post.password',0);
		  $utime=time();

		 $data = new \Think\Model();
		 $result=$data->query("insert into love_user(id,name,password,utime) values
    ($id,'$name','$password','$utime')");

		if(!empty($result)){
			// echo $this->_model->getlastsql();exit;
			 $this->success('添加成功',U('Public/login'));
			}else{
			 $this->error('添加失败');
			}
	}


	// 登陆验证
	public function dologin(){
	  
		 
		$where= array();
		$where['name'] = I('post.name');
		$where['password'] = md5(I('post.password'));
		$Model= M('user');
		$data = $Model->where($where)->select();

		if(!empty($data)){
			$this->success('登录成功',U('index/index'));
			session('love_user',$data[0]);  //设置session
		}else{
			$this->error('密码或账号错误');
			exit;
		}

		//echo $Model->getLastsql(); exit;
	}

}